﻿using System;
using System.Collections.Generic;
using Entities;
using System.Data.Common;
using WebLog;

namespace Data
{
    public sealed class RegionComunneData
    {
        public List<Region> GetListRegions()
        {
            List<Region> regionList = new List<Region>();
            DataBase bd = new DataBase();
            DbDataReader readerDataSource = null;
            try
            {
                bd.Connect();
                bd.BeginTransaction();
                bd.ProcedureSqlName("OBTENER_REGIONES");

                readerDataSource = bd.ExecuteDataReader();
                if (readerDataSource.HasRows)
                {
                    while (readerDataSource.Read())
                    {
                        Region rg = new Region();
                        rg.id = readerDataSource.GetInt32(0);
                        rg.name = readerDataSource.GetString(1);
                        regionList.Add(rg);
                    }
                    readerDataSource.Dispose();
                    readerDataSource.Close();
                }
                else
                {
                    regionList = null;
                }

            }
            catch (Exception ex)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(ex);
                regionList = null;
            }
            finally
            {
                bd.ReleaseResources();
                bd.Disconnect();
            }

            return regionList;
        }

        public List<Commune> GetListComunne(int idPadre)
        {
            List<Commune> listaComunas = new List<Commune>();
            DataBase bd = new DataBase();
            DbDataReader readerDataSource = null;
            try
            {
                bd.Connect();
                bd.BeginTransaction();
                bd.ProcedureSqlName("OBTENER_COMUNAS");
                bd.AssignInt("@idRegion", idPadre);

                readerDataSource = bd.ExecuteDataReader();
                if (readerDataSource.HasRows)
                {
                    while (readerDataSource.Read())
                    {
                        Commune rg = new Commune();
                        rg.id = readerDataSource.GetInt32(0);
                        rg.name = readerDataSource.GetString(1);
                        rg.regionId = readerDataSource.GetInt32(2);
                        listaComunas.Add(rg);
                    }
                    readerDataSource.Dispose();
                    readerDataSource.Close();
                }
                else
                {
                    listaComunas = null;
                }
            }
            catch (Exception ex)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(ex);
                listaComunas = null;
            }
            finally
            {

                bd.ReleaseResources();
                bd.Disconnect();
            }
            return listaComunas;
        }

        public Commune GetComunneById(int idComunne)
        {
            Commune comunne = new Commune();
            DataBase bd = new DataBase();
            DbDataReader readerDataSource = null;
            try
            {
                bd.Connect();
                bd.BeginTransaction();
                bd.ProcedureSqlName("OBTENER_COMUNAS_POR_ID");
                bd.AssignInt("@IDCOMUNA", idComunne);
                readerDataSource = bd.ExecuteDataReader();
                if (readerDataSource.HasRows)
                {
                    while (readerDataSource.Read())
                    {
                        comunne.id = readerDataSource.GetInt32(0);
                        comunne.name = readerDataSource.GetString(1);
                        comunne.regionId = readerDataSource.GetInt32(2);
                    }
                    readerDataSource.Dispose();
                    readerDataSource.Close();
                }
                else
                {
                    comunne = null;
                }
            }
            catch (Exception ex)
            {
                Logger logger = Logger.GetInstance;
                logger.WriteLog(ex);
                comunne = null;
            }
            finally
            {

                bd.ReleaseResources();
                bd.Disconnect();
            }
            return comunne;
        }


    }
}
